package com.example.myblog01.service.impl;

import com.example.myblog01.dao.CommentServiceDao;
import com.example.myblog01.pojo.Comment;
import com.example.myblog01.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceimpl implements CommentService {

    @Autowired
    private CommentServiceDao commentServiceDao;

    @Override
    public List<Comment> listCommentByBlogId(Long blogId) {
        return commentServiceDao.findByBlogId(blogId, Sort.by(Sort.Direction.DESC, "createTime"));
    }

    @Transactional
    @Override
    public Comment saveComment(Comment comment) {
        Long parentCommentId = comment.getParentComment().getId();
//        如果不等于-1 则把他设置为父级
        if (parentCommentId != -1) {
            comment.setParentComment(commentServiceDao.getById(parentCommentId));
        } else {
//            否则 就是一个平常的评论
            comment.setParentComment(null);
        }
        comment.setCreateTime(new Date());
        return commentServiceDao.save(comment);
    }

}
